package cn.oms.manage.controller;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.JsonResult;
import cn.oms.manage.pojo.param.DoctorAddNewParam;
import cn.oms.manage.pojo.param.DoctorUpdateParam;
import cn.oms.manage.pojo.param.MedicineUpdateParam;
import cn.oms.manage.pojo.vo.DoctorListVO;
import cn.oms.manage.pojo.vo.DoctorStandardVO;
import cn.oms.manage.pojo.vo.MedicineListVO;
import cn.oms.manage.pojo.vo.MedicineStandardVO;
import cn.oms.manage.service.IDoctorService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 处理医生相关请求的控制器
 */
@Slf4j
@RestController
@RequestMapping("/doctor")
@Validated
@Api(tags = "03. 医生管理模块")
public class DoctorController {
    @Autowired
    private IDoctorService doctorService;

    public DoctorController() {
        log.debug("创建控制器类对象：DoctorController");
    }

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/doctor/add-new')")
    @ApiOperation("添加医生")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid DoctorAddNewParam doctorAddNewParam) {
        log.debug("开始处理【添加医生】的请求，参数：{}", doctorAddNewParam);
        doctorService.addNew(doctorAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/doctor/delete')")
    @ApiOperation("根据ID删除医生")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的医生ID值！") Long id) {
        log.debug("开始处理【根据ID删除医生】的请求，参数：{}", id);
        doctorService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/doctor/update')")
    @ApiOperation("修改医生详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的医生ID值！") Long id,
                                           @Valid DoctorUpdateParam doctorUpdateParam) {
        log.debug("开始处理【修改药品详情】的请求，ID：{}，新数据：{}", id, doctorUpdateParam);
        doctorService.updateInfoById(id, doctorUpdateParam);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/doctor/read')")
    @ApiOperation("根据ID查询医生详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    public JsonResult<DoctorStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的医生ID值！") Long id) {
        log.debug("开始处理【根据ID查询医生详情】的请求，参数：{}", id);
        DoctorStandardVO queryResult = doctorService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9999/medicines
    @GetMapping("")
    @PreAuthorize("hasAuthority('/doctor/read')")
    @ApiOperation("查询医生列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<DoctorListVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                   String queryType) {
        log.debug("开始处理【查询医生列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<DoctorListVO> pageData;
        if ("all".equals(queryType)) {
            pageData = doctorService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = doctorService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

}
